package algorithm.middle;

/**
 * 641. 设计循环双端队列
 * https://leetcode.cn/problems/design-circular-deque/
 */
public class MyCircularDeque641 {
    /**
     * 使用循环数组模拟双端队列
     *
     */
    int[] arr;
    int head;
    int tail;

    public MyCircularDeque641(int k) {
        arr = new int[k + 1];
        head = 0;
        tail = 0;
    }

    public boolean insertFront(int value) {
        if (isFull()){
            return false;
        }
        head = (head + arr.length - 1) % arr.length;
        arr[head] = value;
        return true;
    }

    public boolean insertLast(int value) {
        if (isFull()){
            return false;
        }
        arr[tail] = value;
        tail = (tail + 1) % arr.length;
        return true;
    }

    public boolean deleteFront() {
        if (isEmpty()){
            return false;
        }
        head = (head + 1) % arr.length;
        return true;
    }

    public boolean deleteLast() {
        if (isEmpty()){
            return false;
        }
        tail = (tail + arr.length - 1) % arr.length;
        return true;
    }

    public int getFront() {
        if (isEmpty()){
            return -1;
        }
        return arr[head];
    }

    public int getRear() {
        if (isEmpty()){
            return -1;
        }
        return arr[(tail + arr.length - 1) % arr.length];
    }

    public boolean isEmpty() {
        return tail == head;
    }

    public boolean isFull() {
        return (tail + 1) % arr.length == head;
    }
}
